<!DOCTYPE html>

<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<title>Example</title>
	    <!--<link rel="shortcut icon" type="image/ico" href="{{STATIC_URL}}assets/images/htyun.ico">-->
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		
		<script type="text/javascript" src="libs/three.js"></script>
		<script type="text/javascript" src="libs/jquery-1.9.0.js"></script>
		<script type="text/javascript" src="libs/stats.js"></script>
		<script type="text/javascript" src="libs/dat.gui.js"></script>
		<script type="text/javascript" src="libs/TrackballControls.js"></script>
		<script type="text/javascript" src="assets/javascripts/bootstrap.js"></script>
		<script type="text/javascript" src="assets/javascripts/pixel-admin.js"></script> 

		<link rel="stylesheet" type="text/css" href="assets/stylesheets/bootstrap.css">
		<link rel="stylesheet" type="text/css" href="assets/stylesheets/pixel-admin.css">
		<link rel="stylesheet" type="text/css" href="assets/stylesheets/widgets.css">
		<link rel="stylesheet" type="text/css" href="assets/stylesheets/rtl.css">
		<link rel="stylesheet" type="text/css" href="assets/stylesheets/themes.css">

		<style >
			body{
				/* set margin to 0 and overflow to hidden, to use the complete page */

				margin: 0;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<!-- Div which will hold the Output -->
		<div id="WebGL-output">
		</div>

		<!-- Div which will show the stats -->
		<div id="Stats-output">
		</div>

		<div style="opacity:0.7;position:absolute;top:50%;left:0px">
			<button id="test" class="btn btn-labled btn-info" onclick="showModal()" >
				<span class="btn-lable icon fa fa-info"></span>
				 Info
			</button><br><br>
			<button id="test1" class="btn btn-labled btn-success" data-toggle="modal" data-target="#searchModal">
				<span class="btn-lable icon fa fa-search"></span>
				Search
			</button>
		</div>
		

		<div id="infoModal" class="modal fade" tabindex="-1" role="dialog" style="display:none;" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div>
						<img src="../image/IMG_0033.JPG" width="400px" height="300px">
					</div>
					<div>
						<p>this is a example~~~</p>
					</div>
				</div>
			</div>
		</div>

		<div id="searchModal" class="modal fade" tabindex="-1" role="dialog" style="display:none;" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-body">
						<div class="input-group no-margin">
							<span class="input-group-addon" style="border:none;background: #fff;background: rgba(0,0,0,.05);"><i class="fa fa-search"></i></span>
							<input type="text" placeholder="Search..." class="form-control no-padding-hr" style="border:none;background: #fff;background: rgba(0,0,0,.05);">
						</div>
					</div>
				</div>
			</div>
		</div>

		<script type="x-shader/x-vertex" id="vertexshader">

			attribute float size;
			attribute vec3 customColor;

			varying vec3 vColor;

			void main() {

				vColor = customColor;

				vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );

				gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );

				gl_Position = projectionMatrix * mvPosition;

			}

		</script>

		<script type="x-shader/x-fragment" id="fragmentshader">

			uniform vec3 color;
			uniform sampler2D texture;

			varying vec3 vColor;

			void main() {

				gl_FragColor = vec4( color * vColor, 1.0 );

				gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );

				if ( gl_FragColor.a < ALPHATEST ) discard;

			}

		</script>

		<!-- Javascript code that runs our Three.js examples -->
		<script type="text/javascript">

			var trackball;
			var clock = new THREE.Clock();

			var uniforms, attributes;

			var PARTICLE_SIZE = 10;

			var raycaster, intersects;
			var mouse;
			var INTERSECTED;

			var sTexture = THREE.ImageUtils.loadTexture("image/IMG_0033.JPG");

			//once everything is loaded, we run our Three.js stuff.
			$(function () { 
 				var stats = initStats();
 				
 				//here we'll put the Three.js stuff
 				var scene = new THREE.Scene();
 				
 				//add camera and renderer
 				var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
 				
 				var renderer = new THREE.WebGLRenderer();
 				renderer.setClearColor(0xEEEEEE, 1.0);
 				renderer.setSize(window.innerWidth, window.innerHeight);
 				renderer.shadowMapEnabled = true;

 				//add Light
 				var ambientLight = new THREE.AmbientLight(0xffffff);
 				scene.add(ambientLight);

		        camera.position.x = 0;
		        camera.position.y = 0;
		        camera.position.z = 330;
		        camera.lookAt(new THREE.Vector3(0, 0, 0));
		        document.getElementById("WebGL-output").appendChild(renderer.domElement);

		        document.addEventListener( 'mousemove', onDocumentMouseMove, false );
		        document.addEventListener( 'click', onDocumentMouseClick, false );
		        window.addEventListener( 'resize', onWindowResize, false );

		        trackball = new THREE.TrackballControls(camera);
				trackball.rotateSpeed = 0.01;
				trackball.zoomSpeed = 0.1;
				trackball.panSpeed = 0.01;
				trackball.minDistance = 330;
				trackball.maxDistance = 400;


		        var geom = new THREE.Geometry();

		        attributes = {
					size:        { type: 'f', value: [] },
					customColor: { type: 'c', value: [] },
				};

				uniforms = {
					color:   { type: "c", value: new THREE.Color( 0xffffff ) },
					texture: { type: "t", value: THREE.ImageUtils.loadTexture( "image/sprites/circle.png" ) }
					
				};

		        var shaderMaterial = new THREE.ShaderMaterial( {
					uniforms: uniforms,
					attributes: attributes,
					vertexShader: document.getElementById( 'vertexshader' ).textContent,
					fragmentShader: document.getElementById( 'fragmentshader' ).textContent,

					alphaTest: 0.9,
				} );

				var radius = 300;
				var rand = 30;		        
		        for(var i = 0; i < 500000; i++){
		        	var particle = initParticle(radius, rand);
		        	geom.vertices.push(particle);
		        }


		        var cloud = new THREE.PointCloud(geom, shaderMaterial);

	            var values_size = attributes.size.value;
				var values_color = attributes.customColor.value;

				var vertices = cloud.geometry.vertices;

				for( var v = 0,  vl = vertices.length; v < vl; v++ ) {
					values_size[ v ] = PARTICLE_SIZE;
					values_color[ v ] = new THREE.Color().setHSL( 0.01 + 0.1 * ( v / vl ), 1.0, 0.5 );
				}

	            scene.add(cloud);

	            //
		        raycaster = new THREE.Raycaster();
		        mouse = new THREE.Vector2();

		        var sphere;
 				//add animation
 				render();
 				function render(){
 					stats.update();

 					var delta = clock.getDelta();
 					trackball.update(delta);

 					raycaster.setFromCamera(mouse, camera);
 					intersects = raycaster.intersectObject(cloud);

 					if ( intersects.length > 0 ) {
						if ( INTERSECTED != intersects[ 0 ].index ) {
							// attributes.size.value[ INTERSECTED ] = PARTICLE_SIZE;
							scene.remove(sphere);
							INTERSECTED = intersects[ 0 ].index;
							sphere = focusParticle();
							sphere.position.x = vertices[INTERSECTED].x;
							sphere.position.y = vertices[INTERSECTED].y;
							sphere.position.z = vertices[INTERSECTED].z;
							scene.add(sphere);
							// console.log(vertices[INTERSECTED]);
							// attributes.size.value[ INTERSECTED ] = PARTICLE_SIZE * 5;
							// attributes.size.needsUpdate = true;
						}
					} else if ( INTERSECTED !== null ) {
						// attributes.size.value[ INTERSECTED ] = PARTICLE_SIZE;
						scene.remove(sphere);
						// attributes.size.needsUpdate = true;
						INTERSECTED = null;
					}

 					requestAnimationFrame(render);
 					renderer.render(scene, camera);
 				};


				function onWindowResize() {

					camera.aspect = window.innerWidth / window.innerHeight;
					camera.updateProjectionMatrix();

					renderer.setSize( window.innerWidth, window.innerHeight );

				}

				function onDocumentMouseMove( event ) {

					event.preventDefault();

					mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
					mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

				}

				var cameraMouse = new THREE.Vector2();
				cameraMouse.x = 0;
				cameraMouse.y = 0;


				functio

				function onDocumentMouseClick( event ){
					if(INTERSECTED != null){
						$("#infoModal").modal("show");
					}
				}

			});

			function initStats(){
				var stats = new Stats();
				stats.setMode(0);
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.lefe = '0px';
				stats.domElement.style.top = '0px';
				stats.domElement.style.opacity = '0.5';
				$("#Stats-output").append(stats.domElement);
				return stats;
			}

			function initParticle(radius, rand){
				var x = 2 * radius * Math.random() - radius;
				var delta = Math.sqrt(radius * radius - x * x);
				var y = 2 * delta * Math.random() - delta;
				var z = Math.sqrt(delta * delta - y * y);
				if(Math.random() > 0.5){
					z = 0 - z;
				}
				return new THREE.Vector3(x + Math.random() * rand - rand / 2, y + Math.random() * rand - rand / 2,
					z + Math.random() * rand - rand / 2);
			}

			function focusParticle(){
				var focusGeom = new THREE.SphereGeometry(2, 50, 50);
				var focusMtl = new THREE.MeshBasicMaterial({color: 0xffffff});
				focusMtl.map = sTexture;
				var focusSphere = new THREE.Mesh(focusGeom, focusMtl);
				focusSphere.name = "focus";
				return focusSphere;
			}

			function showModal(){
				$("#infoModal").modal("show");
			}


		</script>

	</body>
</html>